<?php
/**
 * Ecma-262 5rd 7.8.4
 *
 */
class StringLiteral extends Token {
    
    const DoubleString = 1;
    const SingleString = 2;
    
    public $stringLiteralType;
    
    /**
     * @return the $stringLiteralType
     */
    public function getStringLiteralType() {
        return $this->stringLiteralType;
    }
    
    /**
     * @param field_type $stringLiteralType
     */
    public function setStringLiteralType($stringLiteralType) {
        $this->stringLiteralType = $stringLiteralType;
    }
    
    public function __construct() {
        parent::__construct ();
        $this->tokenType = Token::StringLiteral;
    }
    
    public static function isStringLiteralStart($char){
        if($char == '"'){
            return StringLiteral::DoubleString;
        }
        
        if($char == "'"){
            return StringLiteral::SingleString;
        }
        
        return false;
    }
    
    /**
     * 
     * 获取DoubleStringCharacter 或者 SingleStringCharacter ,参考7.8.4
     * DoubleStringCharacter ::
     *     SourceCharacter but not double-quote " or LineTerminator or backslash \ 
     *     \ EscapeSequence 
     *     \ LineTerminatorSequence
     * 
     * SingleStringCharacter ::
     *     SourceCharacter but not double-quote ' or LineTerminator or backslash \ 
     *     \ EscapeSequence 
     *     \ LineTerminatorSequence
     * 
     * @param boolean $isDouble
     * @param string $stream
     * @param int $pos
     */
    public static function getStringCharacter($stringType,&$stream,$pos){
        $char = $stream[$pos];
        // 非"或者 '
        if($stringType == StringLiteral::DoubleString){
            if($char === '"'){
                return false;
            }
        }
        if($stringType == StringLiteral::SingleString){
            if($char === "'"){
                return false;
            }
        }
        
        // 非LineTerminator
        if(LineTerminator::isLineTerminator($char)){
            return false;
        }
        // 识别\
        if($char === "\\"){
            $nextChar1 = $stream[$pos+1];
            // SingleEscapeCharacter
            if( self::isSingleEscapeCharacter($nextChar1) ){
                return $char.$nextChar1;
            }
            // NonEscapeCharacter
            if( self::isNonEscapeCharacter($nextChar1)){
                return $char.$nextChar1;
            }
            // 0 [lookachead not in DecimalDigit]
            if( $nextChar1 === '0'){
                $nextChar2 = $stream[$pos+2];
                if(!NumericLiteral::isDecimalDigit($nextChar2)){
                    return $char.$nextChar1;
                }
                return false;
            }
            // HexEscapeSequence
            if( $nextChar1 === 'x'){
                $nextChar2 = $stream[$pos+2];
                $nextChar3 = $stream[$pos+3];
                if(NumericLiteral::isHexDigit($nextChar2) && NumericLiteral::isHexDigit($nextChar3)){
                    return $char.$nextChar1.$nextChar2.$nextChar3;
                }
            }
            // UnicodeEscapeSequence
            if( $nextChar1 === 'u'){
                $nextChar2 = $stream[$pos+2];
                $nextChar3 = $stream[$pos+3];
                $nextChar4 = $stream[$pos+4];
                $nextChar5 = $stream[$pos+5];
                if(NumericLiteral::isHexDigit($nextChar2) && NumericLiteral::isHexDigit($nextChar3) && NumericLiteral::isHexDigit($nextChar4) && NumericLiteral::isHexDigit($nextChar5)){
                    return $char.$nextChar1.$nextChar2.$nextChar3.$nextChar4.$nextChar5;
                }
            }
            
            return false;
        }
        
        return $char;
    }
    
    
    public static function isSingleEscapeCharacter($char){
        if(in_array($char, array("'",'"',"\\","b","f","n","r","t","v"))){
            return true;
        }
        
        return false;
    }
    
    public static function isEscapeCharacter($char){
        if(self::isSingleEscapeCharacter($char)){
            return true;
        }
        if(NumericLiteral::isDecimalDigit($char)){
            return true;
        }
        if($char == 'x' || $char == 'u'){
            return true;
        }
        
        return false;
    }
    
    public static function isNonEscapeCharacter($char){
        if(self::isEscapeCharacter($char)){
            return false;
        }
        if(LineTerminator::isLineTerminator($char)){
            return false;
        }
        
        return true;
    }
    
    public static function isHexEscapeSequence(&$stream,$pos){
        if($stream[$pos+1]=='x' && NumericLiteral::isHexDigit($stream[$pos+2]) && NumericLiteral::isHexDigit($stream[$pos+3])){
            return true;
        }
        
        return false;
    }
    
    public static function isUnicodeEscapeSequence(&$stream,$pos){
        if($stream[$pos+1]=='u' && NumericLiteral::isHexDigit($stream[$pos+2]) && NumericLiteral::isHexDigit($stream[$pos+3]) && NumericLiteral::isHexDigit($stream[$pos+4])){
            return true;
        }
        
        return false;
    }
    
    public static function isLineTerminatorSequence(&$stream,$pos){
        
    }
    
}